﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BestLifePOS_Core.DomainObjects;
using BestLifePOS_Core.Data.Fascade;

namespace BestLifePOS_PresentationLayer
{
    public partial class frmGRNApproval : Form
    {
         DataTable GvDatatable1 = new DataTable();
        DataTable GvDatatable = new DataTable();
        DataTable GvPLIDList = new DataTable();
        public frmGRNApproval()
        {
            //check box in dgvGRNApproval
            InitializeComponent();
             DataGridViewCheckBoxColumn check = new DataGridViewCheckBoxColumn();
            check.HeaderText = "Check Item";
            check.FalseValue = "0";
            check.TrueValue = "1";
            check.Width = 20;
            dgvGRNApproval.Columns.Insert(0, check);

            //check box in dgvGRNdetailapproval
            DataGridViewCheckBoxColumn check1 = new DataGridViewCheckBoxColumn();
            check1.HeaderText = "Check Item";
            check1.FalseValue = "0";
            check1.TrueValue = "1";
            check1.Width = 20;
            dgvGRNdetailapproval.Columns.Insert(0, check1);


            #region GvDatatable-Data Table

            GvDatatable.Columns.Add("GRN_ID");
            GvDatatable.Columns.Add("PL_ID");
            GvDatatable.Columns.Add("Pro_Name");
            GvDatatable.Columns.Add("Brand_Name");
            GvDatatable.Columns.Add("Ret_Price");
            GvDatatable.Columns.Add("Pur_Price");
            GvDatatable.Columns.Add("PV");
            GvDatatable.Columns.Add("BV");
            GvDatatable.Columns.Add("Description");
            GvDatatable.Columns.Add("Qty");
            GvDatatable.Columns.Add("Status");
            GvDatatable.Columns.Add("User_Modify");
            GvDatatable.Columns.Add("Last_Modify");
            #endregion


            #region GvPLIDList-Data Table

            GvPLIDList.Columns.Add("GRN_ID");
            GvPLIDList.Columns.Add("PL_ID");
           
            #endregion
        }       



        //First Grid
        public void PendingGRN()
        {
            try
            {
                List<GRN> LvList = new List<GRN>();
                String LvPenList = "PENDING";
                LvList = GRNADO.GetAllGRNbyStatus(LvPenList);
                DataTable LvPRPending = new DataTable();
                dgvGRNApproval.DataSource = LvPRPending;

                #region LvPRPending--Datatable
                //LvPRPending.Columns.Add("Select/Unselect", (typeof(bool)));
                LvPRPending.Columns.Add("GRN_ID");
                LvPRPending.Columns.Add("GRN_Date");
                LvPRPending.Columns.Add("Sup_ID");
                LvPRPending.Columns.Add("Total");
                LvPRPending.Columns.Add("Status");
                LvPRPending.Columns.Add("Branch_ID");
                LvPRPending.Columns.Add("BookGRN_NO");
                LvPRPending.Columns.Add("Sup_Bill_No");
                LvPRPending.Columns.Add("UserID");
                LvPRPending.Columns.Add("DateCreate");
                LvPRPending.Columns.Add("PO_No");
                //LvPRPending.Columns.Add("Po_Id");



                #endregion


                foreach (GRN Lvitem in LvList)
                {
                    LvPRPending = (DataTable)dgvGRNApproval.DataSource;
                    LvPRPending.Rows.Add(LvPRPending.NewRow());
                    LvPRPending.Rows[LvPRPending.Rows.Count - 1][0] = Lvitem.GRN_Id.ToString();
                    LvPRPending.Rows[LvPRPending.Rows.Count - 1][1] = Lvitem.GRN_Date.ToString();
                    LvPRPending.Rows[LvPRPending.Rows.Count - 1][2] = Lvitem.Sup_ID.ToString();
                    LvPRPending.Rows[LvPRPending.Rows.Count - 1][3] = Lvitem.Total.ToString();
                    LvPRPending.Rows[LvPRPending.Rows.Count - 1][4] = Lvitem.status.ToString();
                    LvPRPending.Rows[LvPRPending.Rows.Count - 1][5] = Lvitem.Branch_Id.ToString();
                    LvPRPending.Rows[LvPRPending.Rows.Count - 1][6] = Lvitem.Book_GRNno.ToString();
                    LvPRPending.Rows[LvPRPending.Rows.Count - 1][7] = Lvitem.Sup_Bill_No.ToString();
                    LvPRPending.Rows[LvPRPending.Rows.Count - 1][8] = Lvitem.User_Id.ToString();
                    LvPRPending.Rows[LvPRPending.Rows.Count - 1][9] = Lvitem.Date_Create.ToString();
                    LvPRPending.Rows[LvPRPending.Rows.Count - 1][10] = Lvitem.PO_Id.ToString();
                    

                    //GvPRPending.Rows[7][7]=Lvitem
                }


            }
            catch (Exception)
            {

            }
        }

        private void dgvGRNApproval_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();

            ch1 = (DataGridViewCheckBoxCell)dgvGRNApproval.Rows[dgvGRNApproval.CurrentRow.Index].Cells[0];

            string LvPRid = dgvGRNApproval.Rows[e.RowIndex].Cells[1].Value.ToString();


            if (ch1.Value == null)

                ch1.Value = false;

            switch (ch1.Value.ToString())
            {

                case "True":

                    break;

                case "False":
                    PRDetailFillGrid(LvPRid);//Fill Second Grid


                    break;
            }
        }

        //Second Grid
        private void PRDetailFillGrid(string PPRid)
        {
            String LvStatus="PENDING";
            GvDatatable1 = GRNADO.GetAllGRNDetailsbyGRNid(PPRid, LvStatus);


            for (int i = 0; i <= GvDatatable1.Rows.Count - 1; i++)
            {
                GvDatatable.Rows.Add(GvDatatable.NewRow());
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][0] = GvDatatable1.Rows[i][0].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][1] = GvDatatable1.Rows[i][1].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][2] = GvDatatable1.Rows[i][2].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][3] = GvDatatable1.Rows[i][3].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][4] = GvDatatable1.Rows[i][4].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][5] = GvDatatable1.Rows[i][5].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][6] = GvDatatable1.Rows[i][6].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][7] = GvDatatable1.Rows[i][7].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][8] = GvDatatable1.Rows[i][8].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][9] = GvDatatable1.Rows[i][9].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][10] = GvDatatable1.Rows[i][10].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][11] = GvDatatable1.Rows[i][11].ToString();
                GvDatatable.Rows[GvDatatable.Rows.Count - 1][12] = GvDatatable1.Rows[i][12].ToString();
            }

            dgvGRNdetailapproval.DataSource = GvDatatable;

        }

        private void dgvGRNdetailapproval_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();

            ch1 = (DataGridViewCheckBoxCell)dgvGRNdetailapproval.Rows[dgvGRNdetailapproval.CurrentRow.Index].Cells[0];

            string LvPLid = dgvGRNdetailapproval.Rows[e.RowIndex].Cells[2].Value.ToString();
            //string LvStatus = dgvPOdetailsapproval.Rows[e.RowIndex].Cells[4].Value.ToString();
            string LvGRNid = dgvGRNdetailapproval.Rows[e.RowIndex].Cells[1].Value.ToString();
            if (ch1.Value == null)

                ch1.Value = false;

            switch (ch1.Value.ToString())
            {

                case "True":

                    break;

                case "False":
                    PLListToApprove(LvGRNid, LvPLid);//Add to List


                    break;
            }
        }


        #region Save
        private void PLListToApprove(string PGRNID,string PPLid)
        {
            
            GvPLIDList.Rows.Add(GvPLIDList.NewRow());
            GvPLIDList.Rows[GvPLIDList.Rows.Count - 1][0] = PGRNID;
            GvPLIDList.Rows[GvPLIDList.Rows.Count - 1][1] = PPLid;
          
        }

        private void btnGRNSubmit_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet("podetails");

            string detailsXML = "";

            ds.Tables.Add(GvPLIDList.Copy());
            detailsXML = ds.GetXml();

            PRApprovalList(detailsXML);
            MessageBox.Show("GRN Was Succesfuly Saved");
        }


        private void PRApprovalList(string xmlstring)
        {
              string Lvxml = xmlstring;
            string Lvstatus=null;
            if (rbGRNCancel.Checked)
	{
		 Lvstatus="CANCELLED";
	}
            else
                if (rbGRNApprove.Checked)
	{
		  Lvstatus="APPROVED";
	}

            int Lvresult = GRNADO.UpdateGRN(xmlstring, Lvstatus);
        
            
            
            if (Lvresult != 0 && Lvstatus == "APPROVED")
        {
            MessageBox.Show("GRN Approved Sucessfully");
        }
            else
                 if (Lvresult != 0 && Lvstatus == "CANCELLED")
        {
            MessageBox.Show("GRN Cancelled Sucessfully");
        }
        }

        #endregion

        private void frmGRNApproval_Load(object sender, EventArgs e)
        {
            PendingGRN();
        }

       
      

      
    }
}
